<script lang="ts" setup>
import { ref, reactive, onMounted } from 'vue';
import { ElMessage, FormInstance, FormRules } from 'element-plus';
import useStore from '@/store';
import { props } from './form';
defineOptions({ name: 'OrganizationForm' });
//如const props = defineProps({ name: { type:String, defaule:'' }})
const _props = defineProps(props);
//如const emit = defineEmits(['update', 'delete'])
const buttons = ref([
  {
    icon: 'fluent-save-28-regular',
    fn: () => {
      save();
    },
  },
]);
const save = async () => {
  if (!ruleFormRef.value) return;
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      debugger;
      window.$api.organization.save(form).then(res => {
        ElMessage.success('保存成功!');
        useStore.tab.tabRemove('OrganizationForm' + (form.id ?? ''));
      });
    }
  });
};
const organizations = ref([]);
const organizationTypes = ref([]);
const ruleFormRef = ref<FormInstance>();
const form = reactive({
  id: null,
  pId: null,
  type: 0,
  name: null,
  telephone: null,
  fax: null,
  email: null,
  sort: null,
  remark: null,
});
const rules = reactive<FormRules>({
  name: [
    { required: true, message: '请输入组织机构名称', trigger: 'blur' },
    { max: 50, message: '不能多于50个字符', trigger: 'blur' },
  ],
  type: [{ required: true, message: '请选择组织机构类型', trigger: 'change' }],
});
/** 初始化 */
const init = async () => {
  organizationTypes.value = useStore.dictionary.getComboBoxItems('organizationtype');
  await window.$api.organization.getNoPostOrganization().then(res => {
    organizations.value = res.data;
  });
};
const detail = () => {
  window.$api.organization.detail(_props.window.params.id).then(res => {
    form.id = res.data.id;
    form.pId = res.data.pId;
    form.type = res.data.type;
    form.name = res.data.name;
    form.telephone = res.data.telephone;
    form.fax = res.data.fax;
    form.email = res.data.email;
    form.sort = res.data.sort;
    form.remark = res.data.remark;
  });
};
onMounted(() => {
  init();
  if (_props.window.params) {
    if (_props.window.params.id) {
      detail();
    }
  }
});
</script>

<template>
  <windows-window :window="window">
    <el-form ref="ruleFormRef" :model="form" :rules="rules" label-width="120px" class="demo-ruleForm" size="default" status-icon>
      <el-row>
        <el-col :span="24">
          <el-form-item label="上级机构" prop="PId">
            <el-tree-select
              v-model="form.pId"
              node-key="id"
              :data="organizations"
              :props="{
                children: 'children',
                label: 'name',
              }"
              :expand-on-click-node="false"
              highlight-current
              default-expand-all
              check-strictly
            >
              <template #default="scope">
                <div class="custom-node">
                  <span>{{ scope.node.label }}</span>
                </div>
              </template>
            </el-tree-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="组织机构名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入组织机构名称" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="组织机构类型" prop="type">
            <el-radio-group v-model="form.type">
              <el-radio v-for="item in organizationTypes" :label="item.value">{{ item.name }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="电话号码" prop="telephone">
            <el-input v-model="form.telephone" placeholder="请输入电话号码" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="传真号码" prop="fax">
            <el-input v-model="form.fax" placeholder="请输入电话号码" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="邮箱地址" prop="email">
            <el-input v-model="form.email" placeholder="请输入邮箱地址" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="排序" prop="sort">
            <el-input-number v-model="form.sort" :min="1" controls-position="right" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" type="textarea" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </windows-window>
</template>

<style scoped lang="scss">
#edit {
  height: 100%;
}
.el-form {
  margin: 30px 20px;
}
.custom-node {
  display: flex;
  align-items: center;
  span {
    margin-left: 5px;
  }
}
.select-icon {
  display: flex;
  align-items: baseline;
  flex-wrap: wrap;
  border: 1px solid #dcdfe6;
  border-radius: 5px;
  margin-top: 5px;
  background: #fff;
  width: 100%;
  height: 200px;
  overflow: auto;
  .el-image {
    margin: 10px;
    cursor: pointer;
    border-radius: 5px;
    padding: 5px;
    &:hover,
    &.active {
      background: #dcdfe6;
    }
  }
}
</style>
